from flask import Flask
from flask_restful import reqparse, Resource, Api


"""reqparse
用来帮助我们检验和转换请求数据
"""
app = Flask(__name__)
api = Api(app)


"""
http://127.0.0.1:5000/?rate=aaa&name=aaa
打印:
{
    "message": {
        "rate": "rate cannot be converted"
    }
}
"""
class HelloResource(Resource):
    def get(self):
        # 创建RequestParser对象
        parser = reqparse.RequestParser()
        # 向RequestParser对象中添加需要检验或转换的参数声明
        parser.add_argument('rate', type=int, help='rate cannot be converted', location='args')
        parser.add_argument('name')
        # 使用parse_args()方法启动检验处理
        args = parser.parse_args()
        print(args)
        return {"name":"lisi"}


api.add_resource(HelloResource, '/hello')


if __name__ == '__main__':
    app.run(debug=True)


